package com.elangzhi.ssm.modules.shopEvent.controller;

import com.elangzhi.ssm.controller.AdminBaseController;
import com.elangzhi.ssm.controller.json.Tip;
import com.elangzhi.ssm.controller.util.ParamMap;
import com.elangzhi.ssm.model.Goods;
import com.elangzhi.ssm.model.Shop;
import com.elangzhi.ssm.model.ShopEvent;
import com.elangzhi.ssm.modules.goods.services.GoodsService;
import com.elangzhi.ssm.modules.shopEvent.services.ShopEventService;
import com.elangzhi.ssm.tools.Const;
import com.github.pagehelper.PageInfo;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
* 店铺活动 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/shopEvent")
public class ShopEventController extends AdminBaseController<ShopEvent> {

    @RequestMapping(value="/addEvent")
    public ModelAndView addEvent(ModelMap model,HttpSession session, ShopEvent shopEvent) {
        Shop shop = findShop(session);
        if(shop!=null){
            model.put("shop",shop);
            List<Goods> goodsList = goodsService.listByShopId(shop.getId());
            model.put("goodsList",goodsList);
        }
        return super.add(model, shopEvent);
    }

    private Shop findShop(HttpSession session) {
        Shop shop = (Shop) session.getAttribute(Const.SHOP);
        return shop;
    }

    @Override
    public Tip save(ShopEvent obj) throws Exception {
        obj.setSetTime(new Date());
        return super.save(obj);
    }

    /**
     * 获取我的商品列表 分页
     * 默认第一页，每页指定行数
     * @param request 请求参数
     * @return 实体列表
     * @throws Exception 获取实体列表失败
     */
    @RequestMapping(value="/myList")
    public ModelAndView myList(HttpServletRequest request, HttpSession session, Integer page, Integer size) throws Exception {
        if(page == null || page == 0){
            page = 1;
            size = 10;
        }
        ParamMap paramMap = new ParamMap(request);
        paramMap.put("shopId",findShopId(session));
        PageInfo<ShopEvent> pageInfo =  shopEventService.list(paramMap,page,size);
        paramMap.put("pageInfo",pageInfo);
        return new ModelAndView("admin/shopEvent/list",paramMap);
    }

    /**
     * session中获取店铺信息
     * @return 店铺id
     */
    private Long findShopId(HttpSession session){
        try {
            Shop shop = (Shop) session.getAttribute(Const.SHOP);
            if(shop == null) return null;
            return shop.getId();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取session 店铺信息出错");
            return null;
        }
    }



    //---------------------------- property -------------------------------

    @Resource
    private ShopEventService shopEventService;

    @Resource
    private GoodsService goodsService;


}
